{% extends "base.html" %}

{% block script %}
  <script type="text/javascript">
  	function add(id,name,price)
  	{
  		total_amount_obj = document.getElementById("total_amount");
  		total_price_obj = document.getElementById("total_price");
  		
  		total_amount_obj.innerHTML = parseInt(total_amount_obj.innerHTML) + 1;
  		total_price_obj.innerHTML = parseInt(total_price_obj.innerHTML) + parseInt(price);
  		
  		order_item_count_id = "order_item_count_"+id;
  		order_item_total_id = "order_item_total_"+id;
  		order_item_count_obj = document.getElementById(order_item_count_id);
  		order_item_total_obj = document.getElementById(order_item_total_id);
  		if(order_item_count_obj)
  		{
  			order_item_count_obj.value = parseInt(order_item_count_obj.value) + 1;
  			order_item_total_obj.innerHTML = parseInt(order_item_total_obj.innerHTML) + parseInt(price);
  			return;
  		}
  		
  		tr = "<tr id='"+id+"' value='"+id+"'>"+
  				"<td>"+name+"</td>"+
  				"<td><a id='order_item_price_"+id+"'>"+price+"</a>元/份</td>"+
  				"<td>×<input id='"+order_item_count_id+"' name='"+id+"' value='1' class='input_amount'/>"+
  				"</td>"+
  				"<td>共计<a id='"+order_item_total_id+"'>"+price+"</a>元</td>"+
  				"<td><a class='plus' onclick='plus_one("+id+");'>+</a>"+
  					"<a class='plus' onclick='minus_one("+id+");'>-</a>"+
  				"</td>"+
  				"</tr>";
  		 $("#order_table").append(tr);
  	}
  	function plus_one(id)
  	{
  		price = document.getElementById("order_item_price_"+id).innerHTML;
  		
  		total_amount_obj = document.getElementById("total_amount");
  		total_price_obj = document.getElementById("total_price");
  		
  		total_amount_obj.innerHTML = parseInt(total_amount_obj.innerHTML) + 1;
  		total_price_obj.innerHTML = parseInt(total_price_obj.innerHTML) + parseInt(price);
  		
  		order_item_count_id = "order_item_count_"+id;
  		order_item_total_id = "order_item_total_"+id;
  		order_item_count_obj = document.getElementById(order_item_count_id);
  		order_item_total_obj = document.getElementById(order_item_total_id);
  		price = document.getElementById("order_item_price_"+id).innerHTML;
  		if(order_item_count_obj)
  		{
  			order_item_count_obj.value = parseInt(order_item_count_obj.value) + 1;
  			order_item_total_obj.innerHTML = parseInt(order_item_total_obj.innerHTML) + parseInt(price);
  			return;
  		}
  	}
  	function minus_one(id)
  	{
  		price = document.getElementById("order_item_price_"+id).innerHTML;
  		
  		total_amount_obj = document.getElementById("total_amount");
  		total_price_obj = document.getElementById("total_price");
  		
  		total_amount_obj.innerHTML = parseInt(total_amount_obj.innerHTML) - 1;
  		total_price_obj.innerHTML = parseInt(total_price_obj.innerHTML) - parseInt(price);
  		
  		if(total_amount_obj.innerHTML == 0)
  		{
  			clear_order();
  		}
  		
  		order_item_count_id = "order_item_count_"+id;
  		order_item_total_id = "order_item_total_"+id;
  		order_item_count_obj = document.getElementById(order_item_count_id);
  		order_item_total_obj = document.getElementById(order_item_total_id);
  		price = document.getElementById("order_item_price_"+id).innerHTML;
  		if(order_item_count_obj)
  		{
  			order_item_count_obj.value = parseInt(order_item_count_obj.value) - 1;
  			order_item_total_obj.innerHTML = parseInt(order_item_total_obj.innerHTML) - parseInt(price);
  			if(order_item_count_obj.value == 0)
  			{
  				//document.getElementById("tr_"+id).innerHTML = "";
  				tr_id = id;
  				$("#"+tr_id).remove();
  			}
  		}
  		
  	}
  	function clear_order()
  	{
  		document.getElementById("total_amount").innerHTML=0;
  		document.getElementById("total_price").innerHTML=0;
  		document.getElementById("order_table").innerHTML="";
  	}
  	function check_order()
  	{
  		if(document.getElementById("total_amount").innerHTML==0)
  		{
  			alert("尚未选择任何餐品，无法下单。");
  			return false;
  		}
  		return true;
  	}
  	
  	function submitComment(event)
  	{
  		
  		if(event.ctrlKey&&event.keyCode==13)
        {
        	document.getElementById('submit').click();
        	return false;
        }
  	}
  	
  	function displayReply(comment_id)
  	{
  		if (document.getElementById("reply_div_"+comment_id))
  		{
  			document.getElementById("reply_div_"+comment_id).style.display="block";
  		}
  	}
  	
  	function cancelReplyComment(comment_id)
  	{
  		if (document.getElementById("reply_div_"+comment_id))
  		{
  			document.getElementById("reply_div_"+comment_id).style.display="none";
  		}
  	}
  	
  	function checkComment(textarea_id)
  	{
  		is_blank=true;
  		
  		if (comment_content = document.getElementById(textarea_id))
  		{
  			comment_content = document.getElementById(textarea_id).value;
  			comment_len=comment_content.length;
  		}
  		else
  		{
  			return false;
  		}
  		
  		
  		for(i=0; i<comment_len; i++)
  		{
  			if(comment_content[i]!=' '&&comment_content[i]!='\t'
  			   &&comment_content[i]!='\n'&&comment_content[i]!='\r')
  			{
  				is_blank=false;
  			}
  		}
  		
  		if(is_blank||comment_content==""||comment_content==null||comment_content=="欢迎对该餐馆评论！")
  		{
  			if (textarea_id=='comment-area-id')
  			{
  				alert("请输入评论内容！");
  			}
  			else
  			{
  				alert("请输入回复内容");
  			}
  			
  			return false;
  		}
  	    
  	    return true;
  	}
  	
  	function displayAndHideComment()
  	{
  		if (document.getElementById("comment-ul"))
  		{
  			if (document.getElementById("display-hidden-comment-btn").value=="显示评论")
			{
  				document.getElementById("comment-ul").style.display="block";
  				document.getElementById("display-hidden-comment-btn").value="隐藏评论"
  		 	}
  		 	else
  		 	{
  		 		document.getElementById("comment-ul").style.display="none";
  		 		document.getElementById("display-hidden-comment-btn").value="显示评论"
  		 	}
  		}
  	}
  </script>
{% endblock %}

{% block head %}{{restaurant_v.name}}{% endblock %}
{% block content %}
		
    {% if errors %}
    <p style="color: red;">
            Please correct the error{{ errors }} below.
    </p>
    {% endif %}
    
    <ul id="restaurant_list">
			<li class="restaurant_list_item">
				<table><tr>
					<td class="left">
						<div id="star_{{restaurant_v.id}}" onclick="star_clicked({{restaurant_v.id}});" 
							{% if restaurant_v.loved_by_current %}
							class="star star_on"
							{% else %}
							class="star star_off"
							{% endif %}	
						>&nbsp
						</div>
						<a class="restaurant_name">{{restaurant_v.name}}</a>
					<p>
						<a class="restaurant_addr">{{restaurant_v.address}}</a>
					</p>
					<p>
						<a class="restaurant_desc">{{restaurant_v.phone}}</a>
					</p>
					</td>
					<td class="right">
						<div id="status">
							{% if restaurant_v.is_open %}
								<a>营业中</a>
							{% else %}
								<a>暂停营业</a>
							{% endif %}
						</div>
						<div id="link">
						</div>
					</td>
				</tr></table>
			</li>
		</ul>
    
    <table id="restaurant_menu">
    {% for c in categories %}
        <tr><td><a class="category_name">{{c.name}}</a></td></tr>
        {% for i in c.get_menu_items %}
            <tr class="item_div">
            	<td>
            		<a class="plus" onclick="add({{i.id}},'{{i.name}}',{{i.price}});">+</a>
            		<a>{{i.name}}</a>
            	</td>
            	<td><a>{{i.price}}</a></td>
            </tr>
        {% endfor %}
    {% endfor %}
    </table>
    
    {% include "comment/restaurant_comment.html" %}
{% endblock %}




{% block sidebar%}
	<div id="order">
	<h3 id="order_title">已选餐品(<a id="total_amount">0</a>份，共计<a id="total_price">0</a>元)</h3>
	<form id="order_form" name="order_form" class="" method="post" action="/create_order/">
		{% csrf_token %}
		<table id="order_table" name="order_table">
		
		</table>
		<input onclick="clear_order();" class="button" type="button" value="清空"/>
		<input class="button" type="submit" onclick="return check_order();" value="确认订单"/>
		<input type="hidden" id="restaurant_id" name="restaurant_id" value="{{restaurant_v.id}}">
	</form>
</div>
{% endblock%}